package cn.com.lms.business.exam.service;

import cn.com.lms.business.exam.enums.SubjectTypeEnum;
import cn.com.lms.business.exam.persistence.entity.QuestionSubject;
import cn.com.lms.business.exam.persistence.entity.QuestionSubjectFill;
import cn.com.lms.business.exam.persistence.repository.QuestionSubjectFillRepository;
import com.alibaba.fastjson.JSON;
import com.bnzj.core.persistence.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionSubjectFillService extends BaseService<QuestionSubjectFillRepository, QuestionSubjectFill,Long> {

    public void batchSave(QuestionSubject questionSubject,List<QuestionSubjectFill> questionSubjectFillList){

        if (!CollectionUtils.isEmpty(questionSubjectFillList) && questionSubject!=null && SubjectTypeEnum.FILL.getValue().equals(questionSubject.getType()) ) {
            List<QuestionSubjectFill> questionSubjectFills = questionSubjectFillList.stream().map(item -> {
                QuestionSubjectFill questionSubjectFill = new QuestionSubjectFill();
                questionSubjectFill.setAnswerList(item.getAnswerList());
                questionSubjectFill.setAnswer(item.getAnswer());
                questionSubjectFill.setSerialId(item.getSerialId());
                questionSubjectFill.setSubjectId(questionSubject.getId());
                questionSubjectFill.setAnswer(JSON.toJSONString(questionSubjectFill.getAnswerList()));
                questionSubjectFill.setCreator(questionSubject.getCreator());
                questionSubjectFill.setCreateTime(questionSubject.getCreateTime());
                questionSubjectFill.setCreatorName(questionSubject.getCreatorName());
                return questionSubjectFill;
            }).collect(Collectors.toList());
            super.saveAll(questionSubjectFills);
        }
    }
}
